package com.play.ballen.me.ui

import android.content.Context
import android.content.Intent
import android.view.View
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.play.ballen.config.KVConst
import com.play.ballen.databinding.ActivityWithDrawBinding
import com.play.ballen.me.dialog.InputPayPwdDialogUtils
import com.play.ballen.me.dialog.ModifyNickNameDialogUtils
import com.play.ballen.me.dialog.OperateSuccessDialogUtils
import com.play.ballen.me.vm.WithDrawVM
import com.yuven.appframework.base.BaseStdTitleActivity
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.baselib.utils.ResourceUtil

class WithDrawActivity : BaseStdTitleActivity<WithDrawVM, ActivityWithDrawBinding>() {

    private val inputPayPwdDialogUtils by lazy {
        InputPayPwdDialogUtils(mContext)
    }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, WithDrawActivity::class.java)
            context.startActivity(starter)
        }
    }

    private val operateSuccessDialogUtils by lazy { OperateSuccessDialogUtils(this) }

    override fun viewModelClass(): Class<WithDrawVM> {
        return WithDrawVM::class.java
    }

    override fun contentLayoutId(): Int {
        return R.layout.activity_with_draw
    }

    override fun initObserver() {
        super.initObserver()

        viewModel.withDrawInfo.observe(this) {
            it.run {
                dataBinding.edExtraCount.hint =
                    ResourceUtil.getString(R.string.extra_amount_least, least)
                dataBinding.tvAvailableCount.text =
                    ResourceUtil.getString(R.string.extra_available_amount_tip, money)

                dataBinding.tvServicePrice.text = fee
                dataBinding.tvDesc.text = memo
            }
        }

        viewModel.actionCommitSuccess.observe(this) {
            operateSuccessDialogUtils.show(ResourceUtil.getString(R.string.extra_dialog_tip))
            //刷新页面
            viewModel.getWithDrawInfo()
        }
    }

    override fun onToolbarRightIconClick() {
        MoneyLogActivity.start(
            this,
            ResourceUtil.getString(R.string.extra_record_title),
            KVConst.MONEY_LOG_TYPE_WITHDRAW,
            ""
        )
    }

    override fun initData() {
        viewModel.getWithDrawInfo()

        val rightIcon = getTitleBarLayout().getRightIcon()
        rightIcon?.visibility = View.VISIBLE
        rightIcon?.setImageResource(R.mipmap.ic_me_recharge_detail)

        dataBinding.tvExtraAll.setOnSingleClickListener tag@{
            if (viewModel.withDrawInfo.value == null) {
                return@tag
            }
            dataBinding.edExtraCount.setText(viewModel.withDrawInfo.value!!.money)
        }

        dataBinding.btnExtra.setOnSingleClickListener tag@{
            val address = dataBinding.edExtraAddress.text.toString().trim()
            if (address.isEmpty()) {
                ToastUtils.show(ResourceUtil.getString(R.string.extra_address_empty_tip))
                return@tag
            }

            val count = dataBinding.edExtraCount.text.toString().trim()
            if (count.isEmpty()) {
                ToastUtils.show(dataBinding.edExtraCount.hint)
                return@tag
            }

            inputPayPwdDialogUtils.show { pwd ->
                viewModel.commitWithDraw(pwd, address, count)
            }
        }
    }

    override fun initIntent(intent: Intent?) {
    }

    override fun getToolBarTitle(): String = ResourceUtil.getString(R.string.extra_title)
}